﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly: WebResource("Acms.WebControls.GoogleMap.js", "application/x-javascript")]
namespace Acms.WebControls
{
    public class GoogleMap : WebControl
    {
        public float? Latitude { get; set; }
        public float? Longitude { get; set; }
        public string Address { get; set; }
        public string ApiKey { get; set; }
        public Unit MapWidth { get; set; }
        public Unit MapHeight { get; set; }
        public int Zoom { get; set; }
        public string IconUrl { get; set; }
        public int iconWidth;
        public int iconHeight;
        public int iconAnchorX;
        public int iconAnchorY;

        protected override void OnPreRender(EventArgs e)
        {
            StringBuilder startUp = new StringBuilder();
            startUp.Append("createMap(");
            startUp.AppendFormat("\"{0}_Map\"", this.ClientID);
            startUp.Append(",");
            if (Latitude.HasValue)
                startUp.Append(Latitude.Value);
            else
                startUp.Append("null");
            startUp.Append(",");
            if(Longitude.HasValue)
                startUp.Append(Longitude.Value);
            else
                startUp.Append("null");
            startUp.Append(",");
            startUp.AppendFormat("\"{0}\"", Address);
            startUp.Append(",");
            if (Zoom >= 0 && Zoom <= 18)
            {
                startUp.Append(Zoom);
            }
            else
            {
                startUp.Append(16);
            }
            startUp.Append(",");
            if (!string.IsNullOrEmpty(IconUrl))
            {
                startUp.AppendFormat("\"{0}\"", Page.ResolveUrl(IconUrl));
            }
            else
            {
                startUp.Append("null");
            }
            startUp.AppendFormat(", {0}, {1}, {2}, {3});", iconWidth, iconHeight, iconAnchorX, iconAnchorY);

            string tmpApiKey = ApiKey;
            if (string.IsNullOrEmpty(tmpApiKey))
            {
                tmpApiKey = System.Configuration.ConfigurationManager.AppSettings["GoogleMapsKey"];
            }
            Page.ClientScript.RegisterClientScriptInclude("googlemaps", string.Concat("http://maps.google.com/maps?file=api&v=2&key=", Page.Server.UrlEncode(tmpApiKey)));
            Page.ClientScript.RegisterClientScriptResource(this.GetType(), "Acms.WebControls.GoogleMap.js");
            Page.ClientScript.RegisterStartupScript(this.GetType(), this.ClientID, startUp.ToString(), true);

            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, string.Concat("googleMap ", this.CssClass).Trim());
            writer.RenderBeginTag(HtmlTextWriterTag.Div);//wrapper div

            writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Concat(this.ClientID, "_Map"));
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "map");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, string.Format("width:{0};height:{1};", MapWidth, MapHeight));
            writer.RenderBeginTag(HtmlTextWriterTag.Div);//map div

            writer.RenderEndTag();//map div

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "directions_wrapper");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);//directions div

            writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Concat(this.ClientID, "_GetDirections"));
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "directions_get");
            writer.AddAttribute("onkeypress", string.Concat("javascript:return WebForm_FireDefaultButton(event, '", this.ClientID, "_btnDirections')"));
            writer.RenderBeginTag(HtmlTextWriterTag.Div);//get directions div

            writer.AddAttribute(HtmlTextWriterAttribute.For, string.Concat(this.ClientID, "_txtDirections"));
            writer.RenderBeginTag(HtmlTextWriterTag.Label);
            writer.Write("Your Post Code :");
            writer.RenderEndTag();//label

            writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Concat(this.ClientID, "_txtDirections"));
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();//input

            string tmpAddress = null;
            if(Longitude.HasValue && Latitude.HasValue)
            {
                tmpAddress = string.Concat(Latitude.Value, ", ", Longitude.Value);
            }
            else
            {
                tmpAddress = Address;
            }

            writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Concat(this.ClientID, "_lnkDirections"));
            writer.AddAttribute(HtmlTextWriterAttribute.Target, "_Blank");
            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, string.Format("javascript:this.href = \"http://maps.google.co.uk/maps?saddr=\" + document.getElementById(\"{1}_txtDirections\").value + \"&daddr={0}\";", HttpUtility.UrlEncode(tmpAddress), this.ClientID));
            writer.AddAttribute(HtmlTextWriterAttribute.Href, string.Format("http://maps.google.co.uk/maps?daddr={0}", HttpUtility.UrlEncode(tmpAddress)));
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write("Get Directions");
            writer.RenderEndTag();
            /*
            writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Concat(this.ClientID, "_btnDirections"));
            writer.AddAttribute(HtmlTextWriterAttribute.Value, "Get Directions");
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
            
            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, string.Format("return getDirections('{0}_Map', '{0}_txtDirections', '{0}_Directions', '{1}');", this.ClientID, tmpAddress));
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();//button
            */
            writer.RenderEndTag();//get directions div


            writer.AddAttribute(HtmlTextWriterAttribute.Class, "directions_google");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Concat(this.ClientID, "_Directions"));
            writer.RenderBeginTag(HtmlTextWriterTag.Div);//google directions div
            writer.RenderEndTag();

            writer.RenderEndTag();//directions div

            writer.RenderEndTag();//wrapper div

        }
    }
}
